<?php
    if($_SERVER['REQUEST_METHOD'] == 'POST'){$pr=1;}
    else{$pr=0;}
	// Invoer Controle
	if ($pr)
	{
        $error = 0;
        // naam controle
		if (empty($_POST['lastname'])){$error = 1;}
        // e-mail controle
        if (empty($_POST['email'])){$error = 1;}
        elseif (function_exists('filter_var') && !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){$email_error = $error = 1;}
        elseif ($_POST['email'] != $_POST['emailconfirmation']){$emailconfirmation_error = 1;}
        // geboortedatum controle
		if ((empty($_POST['bdday'])+empty($_POST['bdmonth'])+empty($_POST['bdyear']))!=0){$bd_empty = $error=1;}
		elseif (!checkdate($_POST['bdmonth'],$_POST['bdday'],$_POST['bdyear'])){$bd_error = $error = 1;}
		else {
			// leeftijd calculator
			$birthday = $_POST['bdday'].'-'.$_POST['bdmonth'].'-'.$_POST['bdyear'];
			$today = date('d-m-Y');

			$a_birthday = explode('-', $birthday);
			$a_today = explode('-', $today);

			$day_birthday = $a_birthday[0];
			$month_birthday = $a_birthday[1];
			$year_birthday = $a_birthday[2];
			$day_today = $a_today[0];
			$month_today = $a_today[1];
			$year_today = $a_today[2];

			$age = $year_today - $year_birthday;

			if (($month_today < $month_birthday) || ($month_today == $month_birthday && $day_today < $day_birthday))
			{
				$age--;
			}
			//leeftijd controlle
			if ($age<=16){$age_error = $error = 1;}
		}
		// telefoonnummer controle
        if (empty($_POST['phone'])){$error = 1;}
        elseif (!preg_match("/^0[0-9]{9}$/i", $_POST['phone'])){$phone_error = $error = 1;}
		// poscode controle
		if (empty($_POST['zipcode'])){$error = 1;}
		elseif (!preg_match("/^[1-9][0-9]{3}[A-Za-z]{2}$/i", $_POST['zipcode'])){$zipcode_error = $error = 1;}
		// huisnummer controle
		if (empty($_POST['number'])){$error = 1;}
		elseif (!preg_match("/^[0-9]{1,}$/i", $_POST['number'])){$number_error = $error = 1;}
		// Straat controle
		if (empty($_POST['street'])){$error = 1;}
		// Woonplaats controle
		if (empty($_POST['city'])){$error = 1;}
	}

	// Laat forum zien als niet alles (juist) is ingevoerd.
	if (($pr && !empty($error)) || $_SERVER['REQUEST_METHOD'] == 'GET')
	{
		// Het formulier
		// Persoonlijke gegevens
		echo '<form method="post" action="' . $_SERVER['REQUEST_URI'] . '"><label for="lastname">Achternaam: </label><input type="text" id="lastname" maxlength="50" name="lastname" value="'.(isset($_POST['lastname'])?htmlspecialchars($_POST['lastname']):'') . '" /><br />';
            if ($pr && empty($_POST['lastname'])){echo'<span style="color:red;">Vul uw achternaam in</span><br />';}
            else {echo'<br />';}
        echo '<label for="phone">(Mobiele) Telefoon: </label><input type="text" id="phone" maxlength="10" name="phone" value="'.(isset($_POST['phone'])?htmlspecialchars($_POST['phone']):'') . '" /><br />';
            if ($pr && empty($_POST['phone'])){echo'<span style="color:red;">Vul uw telefoon in</span><br />';}
            elseif ($pr && !empty($phone_error)){echo '<span style="color:red;">Geef een geldig telefoonnummer op</span><br />';}
            else {echo '<br />';}
		echo '<label for="email">E-mailadres: </label><input type="text" id="email" maxlength="50" name="email" value="'.(isset($_POST['email'])?htmlspecialchars($_POST['email']) :'').'" /><br />';
            if ($pr && empty($_POST['email'])){echo'<span style="color:red;">Vul uw e-mailadres in</span><br />';}
            elseif ($pr && !empty($email_error)){echo '<span style="color:red;">Geef een geldig e-mailadres op</span><br />';}
            else {echo'<br />';}
         echo '<label for="emailconfirmation">Bevestig e-mailadres: </label><input type="text" id="emailconfirmation" maxlength="100" name="emailconfirmation" value="'.(isset($_POST['emailconfirmation'])?htmlspecialchars($_POST['emailconfirmation']) :'').'" /><br />';
            if ($pr && empty($_POST['emailconfirmation'])){echo '<span style="color:red;">E-mailadres bevestigen is verplicht</span><br />';}
            elseif ($pr && !empty($emailconfirmation_error)){'<span style="color:red;">De ingevulde e-mail adressen moeten gelijk zijn</span><br />';}
            else {echo'<br />';}
		echo '<label for="birthdate">Geboortedatum: </label><select name="bdday"><option value="0">- Dag -</option>';
		for ($i=1; $i<=31; $i++){echo'<option '; if($pr && $i == $_POST['bdday']){echo'selected="selected"';} echo'value="'.$i.'">'.$i.'</option>';}
		echo '</select><select name="bdmonth"><option value="0">- Maand -</option>';
		for ($i=1; $i<=12; $i++){echo'<option '; if($pr && $i == $_POST['bdmonth']){echo'selected="selected"';} echo'value="'.$i.'">'.$i.'</option>';}
		echo '</select><select name="bdyear"><option value="0">- Jaar -</option>';
		for ($i=date("Y"); $i>=1900; $i--){echo'<option '; if($pr && $i == $_POST['bdyear']){echo'selected="selected"';} echo'value="'.$i.'">'.$i.'</option>';}
		echo '</select><br />';
			if ($pr && !empty($bd_empty)){echo'<span style="color:red;">Vul uw geboortedatum in</span><br />';}
			elseif ($pr && !empty($bd_error)){echo'<span style="color:red;">Vul een juiste geboortedatum in</span><br />';}
			elseif ($pr && !empty($age_error)){echo'<span style="color:red;">U moet minimaal 16 jaar oud zijn om te kunnen bestellen</span><br />';}
			else {echo'<br />';}
	    // Adres gegevens
        echo '<label for="zipcode">Postcode: </label><input type="text" id="zipcode" maxlength="6" name="zipcode" value="'.(isset($_POST['zipcode'])?htmlspecialchars($_POST['zipcode']):'') . '" /><br />';
			if ($pr && empty($_POST['zipcode'])){echo '<span style="color:red;">Vul uw postcode in</span><br />';}
			elseif ($pr && !empty($zipcode_error)){echo '<span style="color:red;">Geef een geldige postcode op</span><br />';}
			else {echo'<br />';}
		echo '<label for="number">Huisnummer: </label><input type="text" id="number" maxlength="10" name="number" value="'.(isset($_POST['number'])?htmlspecialchars($_POST['number']):'') . '" /><br />';
			if ($pr && empty($_POST['number'])){echo '<span style="color:red;">Vul uw huisnummer in</span><br />';}
			elseif ($pr && !empty($number_error)){echo '<span style="color:red;">Geef een geldig huisnummer op</span><br />';}
			else {echo'<br />';}
		echo '<label for="addition">Aanvulling: </label><input type="text" id="addition" maxlength="10" name="addition" value="'.(isset($_POST['addition'])?htmlspecialchars($_POST['addition']):'') . '" /><br /><br />';
		echo '<label for="street">Straat: </label><input type="text" id="straat" maxlength="50" name="street" value="'.(isset($_POST['street'])?htmlspecialchars($_POST['street']):'') . '" /><br />';
			if ($pr && empty($_POST['street'])){echo '<span style="color:red;">Vul uw straat in</span><br />';}
			else {echo'<br />';}
		echo '<label for="city">Woonplaats: </label><input type="text" id="city" maxlength="50" name="city" value="'.(isset($_POST['city'])?htmlspecialchars($_POST['city']):'') . '" /><br />';
			if ($pr && empty($_POST['city'])){echo '<span style="color:red;">Vul uw woonplaats in</span><br />';}
			else {echo'<br />';}
		echo '<input type="submit" name="submit" value=" Versturen " /></form>';
    }
	// Uitvoer als alles (juist) is ingevoerd.
	else
	{
		echo '<h1>Succes</h1>
		<p>Het formulier is goed ingevuld.</p>';
	}
?>